package com.msg.service.Impl;

import com.msg.service.EmailService;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @author lxx
 */
@Service
@RequiredArgsConstructor
public class EmailServiceImpl implements EmailService {


    private final JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public boolean sendSimpleMail(String to, String subject, String content) {

        //创建简单邮件消息
        SimpleMailMessage message = new SimpleMailMessage();
        //谁发的
        message.setFrom(from);
        //谁要接收
        message.setTo(to);
        //邮件标题
        message.setSubject(subject);
        //邮件内容
        message.setText(content);
        try {
            mailSender.send(message);
            logger.info("发送普通邮件成功");
            return true;
        } catch (MailException e) {
            e.printStackTrace();
            logger.error("发送普通邮件时发生异常！", e);
            return false;
        }

    }

    @Override
    public boolean sendHtmlMail(String to, String subject, String content) {
        MimeMessage message = mailSender.createMimeMessage();

        try {
            //true表示需要创建一个multipart message
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            mailSender.send(message);
            logger.info("html邮件发送成功");
            return true;
        } catch (MessagingException e) {
            logger.error("发送html邮件时发生异常！", e);
            return false;
        }

    }

    @Override
    public boolean sendAttachmentsMail(String to, String subject, String content, File file){
        MimeMessage message = mailSender.createMimeMessage();

        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            FileSystemResource fileres = new FileSystemResource(file);
            helper.addAttachment(file.getName(), fileres);

            mailSender.send(message);
            logger.info("带附件的邮件已经发送。");
            return true;
        } catch (MessagingException e) {
            logger.error("发送带附件的邮件时发生异常！", e);
            return false;
        }
    }

}
